import { connect } from 'react-redux'
import { switchMenu, saveBtnList } from './../../redux/action'
import React, { Component } from 'react'
import menoConfig from './../../config/menuConfig'
import {Menu } from 'antd';
import {NavLink} from 'react-router-dom'

const {SubMenu} = Menu
class Navleft extends Component {
    state = {
        currentKey: ''
    }
    // 菜单点击
    handleClick = ({ item, key }) => {
        // console.info(item, '')
        if (key == this.state.currentKey) {
            return false;
        }
        // 事件派发，自动调用reducer，通过reducer保存到store对象中
        const { dispatch } = this.props;
        dispatch(switchMenu(item.props.title));

        this.setState({
            currentKey: key
        });
        // hashHistory.push(key);
    };
    componentWillMount() {
        let menoTreeNode  = this.renderMeno(menoConfig)
        this.setState({
            menoTreeNode
        })
    }
    renderMeno = (data) => {
        return data.map((item) => {
            if(item.children) {
                return <SubMenu title={item.title} key={item.key}>
                    {this.renderMeno(item.children)}
                </SubMenu>
            }
            return <Menu.Item title={item.title} key={item.key}>
                <NavLink to={item.key}> {item.title}</NavLink>
               
            </Menu.Item>
        })
    }
    homeHandleClick = () => {
        const { dispatch } = this.props;
        dispatch(switchMenu('首页'));
        this.setState({
            currentKey: ""
        });
    };
    render() {
        return (
            <div>
                 {/* <NavLink to="/home" onClick={this.homeHandleClick}>
                    <div className="logo">
                        <img src="/assets/logo-ant.svg" alt=""/>
                        <h1>Imooc MS</h1>
                    </div>
                </NavLink> */}
                <Menu theme="dark"   onClick={this.handleClick}>
                    {this.state.menoTreeNode}
                </Menu>
            </div>
        )
    }
}
export default connect()(Navleft)